<?php
namespace app\api\controller;
use app\common\controller\Api;
use think\Controller;
use app\admin\command\Api\library\Extractor;
use app\admin\model\AuthGroup;
use app\common\controller\Backend;
use fast\Tree;
use think\Cache;
use fast\Random;
use fast\Cols;
use fast\Date;
/**
 * 首页接口
 */
class Doc extends Controller
{
	protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 初始化操作
     * @access public
     */
    public function initialize(){
        //移除HTML标签
        //$this->request->filter('strip_tags');
        $modulename = $this->request->module();
        $controllername = strtolower($this->request->controller());
        $actionname = strtolower($this->request->action());
        //$upload = \app\common\model\Config::upload();
    }
    
    /**
	* 接口文档首页
	* @method  post
	* @param   string  $username   用户名
	* @param   string  $password   密码
	* @param   int     $keeptime   有效时长
	* @return  返回文档列表
	* @returnparam tstr string 有效时长,低效时长 
	* @returnparam result tinyint 1,成功0，失败
	* @returnobject City
	* @returnparam stred tinyint 3,成功4，失败
	* @returnparam strcc string 字符串
	*/
    public function index(){
        if (version_compare(PHP_VERSION, '7.0.0', '<')) {
            if (extension_loaded('Zend OPcache')) {
                $configuration = opcache_get_configuration();
                $directives = $configuration['directives'];
                $configName = request()->isCli() ? 'opcache.enable_cli' : 'opcache.enable';
                if (!$directives[$configName]) {
                    throw new Exception("Please make sure {$configName} is turned on, Get help:https://forum.fastadmin.net/d/1321");
                }
            } else {
                throw new Exception("Please make sure opcache already enabled, Get help:https://forum.fastadmin.net/d/1321");
            }
        }
        $moduleDir = ROOT_PATH . 'application' . DS .'api' . DS . 'controller' . DS;
        if (!is_dir($moduleDir)) {
            throw new Exception('module not found');
        }
        $files = new \RecursiveIteratorIterator(
            new \RecursiveDirectoryIterator($moduleDir), \RecursiveIteratorIterator::LEAVES_ONLY
        );
        foreach ($files as $name => $file) {
            if (!$file->isDir()) {
                $filePath = $file->getRealPath();
                $filename = $file->getFileName();//获取文件名
                //echo $filename;exit
                if($filename=='Common.php'||$filename=='Doc.php'){
                    continue;
                }
                $contents = file_get_contents($filePath);
                $namespace = $class = "";
                $getting_namespace = false;$getting_class = false;
                foreach (token_get_all($contents) as $token) {
                    //If this token is the namespace declaring, then flag that the next tokens will be the namespace name
                    if (is_array($token) && $token[0] == T_NAMESPACE) {
                        $getting_namespace = true;
                    }
                    //If this token is the class declaring, then flag that the next tokens will be the class name
                    if (is_array($token) && $token[0] == T_CLASS) {
                        $getting_class = true;
                    }
                    //While we're grabbing the namespace name...
                    if ($getting_namespace === true) {
                        //If the token is a string or the namespace separator...
                        if (is_array($token) && in_array($token[0], [T_STRING, T_NS_SEPARATOR])) {
                            //Append the token's value to the name of the namespace
                            $namespace .= $token[1];
                        } else if ($token === ';') {
                            //If the token is the semicolon, then we're done with the namespace declaration
                            $getting_namespace = false;
                        }
                    }
                    //While we're grabbing the class name...
                    if ($getting_class === true) {
                        //If the token is a string, it's the name of the class
                        if (is_array($token) && $token[0] == T_STRING) {
                            //Store the token's value as the class name
                            $class = $token[1];
                            //Got what we need, stope here
                            break;
                        }
                    }
                }
                $classes[] = $namespace ? $namespace . '\\' . $class : $class;
            }
        }
        //print_r($classes);exit;
        $output = array();
        $c = 0;
        $parentRuleList = array();
        foreach ($classes as $class){
            //获取了控制器函数的注释
            $output = Extractor::getApiClassAnnotations($class);
            //print_r(json_encode($output));exit;
            foreach ($output as $k => $v) {
                if($k=='initialize'||$k=='loadlang'){
                    continue;
                }
                //$parentRuleList[$k] = $v;
                //print_r(json_encode($v));
                if(count($v)>0){
                    foreach ($v as $kk => $vv) {
                        $ary = array();
                        //print_r($vv);
                        if(!isset($vv['ApiMethod'])){
                            continue;
                        }
                        $ary['title'] = isset($vv['ApiTitle'])?$vv['ApiTitle'][0]:'';
                        $ary['method'] = isset($vv['ApiMethod'])?$vv['ApiMethod'][0]:'';
                        $ary['url'] = isset($vv['ApiRoute'])?$vv['ApiRoute'][0]:'';
                        if($ary['title']!=''){
                            $parentRuleList[$k][$kk] = $ary;
                        }
                        
                    } 
                }
            }
        }
        $this->view->assign("list", $parentRuleList);
        $routeurl = array();
        $routeurl['app\api\controller\Demo'] = '示例';
        $routeurl['app\api\controller\Ems'] = '邮件验证';
        $routeurl['app\api\controller\Index'] = '主页';
        $routeurl['app\api\controller\Sms'] = '短信验证';
        $routeurl['app\api\controller\Token'] = 'Token';
        $routeurl['app\api\controller\User'] = '会员管理';
        $routeurl['app\api\controller\Validate'] = '数据验证';
        $this->view->assign("routeurl",$routeurl);
    	return $this->view->fetch('index');
    }

    public function info(){
        if (version_compare(PHP_VERSION, '7.0.0', '<')) {
            if (extension_loaded('Zend OPcache')) {
                $configuration = opcache_get_configuration();
                $directives = $configuration['directives'];
                $configName = request()->isCli() ? 'opcache.enable_cli' : 'opcache.enable';
                if (!$directives[$configName]) {
                    throw new Exception("Please make sure {$configName} is turned on, Get help:https://forum.fastadmin.net/d/1321");
                }
            } else {
                throw new Exception("Please make sure opcache already enabled, Get help:https://forum.fastadmin.net/d/1321");
            }
        }
        $moduleDir = ROOT_PATH . 'application' . DS .'api' . DS . 'controller' . DS;
        if (!is_dir($moduleDir)) {
            throw new Exception('module not found');
        }
        $files = new \RecursiveIteratorIterator(
            new \RecursiveDirectoryIterator($moduleDir), \RecursiveIteratorIterator::LEAVES_ONLY
        );
        foreach ($files as $name => $file) {
            if (!$file->isDir()) {
                $filePath = $file->getRealPath();
                $filename = $file->getFileName();//获取文件名
                //echo $filename;exit
                if($filename=='Common.php'||$filename=='Doc.php'){
                    continue;
                }
                $contents = file_get_contents($filePath);
                $namespace = $class = "";
                $getting_namespace = false;$getting_class = false;
                foreach (token_get_all($contents) as $token) {
                    //If this token is the namespace declaring, then flag that the next tokens will be the namespace name
                    if (is_array($token) && $token[0] == T_NAMESPACE) {
                        $getting_namespace = true;
                    }
                    //If this token is the class declaring, then flag that the next tokens will be the class name
                    if (is_array($token) && $token[0] == T_CLASS) {
                        $getting_class = true;
                    }
                    //While we're grabbing the namespace name...
                    if ($getting_namespace === true) {
                        //If the token is a string or the namespace separator...
                        if (is_array($token) && in_array($token[0], [T_STRING, T_NS_SEPARATOR])) {
                            //Append the token's value to the name of the namespace
                            $namespace .= $token[1];
                        } else if ($token === ';') {
                            //If the token is the semicolon, then we're done with the namespace declaration
                            $getting_namespace = false;
                        }
                    }
                    //While we're grabbing the class name...
                    if ($getting_class === true) {
                        //If the token is a string, it's the name of the class
                        if (is_array($token) && $token[0] == T_STRING) {
                            //Store the token's value as the class name
                            $class = $token[1];
                            //Got what we need, stope here
                            break;
                        }
                    }
                }
                $classes[] = $namespace ? $namespace . '\\' . $class : $class;
            }
        }
        //print_r($classes);exit;
        $output = array();
        $c = 0;
        $parentRuleList = array();
        foreach ($classes as $class){
            //获取了控制器函数的注释
            $output = Extractor::getApiClassAnnotations($class);
            //print_r(json_encode($output));exit;
            $info = array();
            foreach ($output as $k => $v) {
                if($k=='initialize'||$k=='loadlang'){
                    continue;
                }
                //$parentRuleList[$k] = $v;
                //print_r(json_encode($v));
                if(count($v)>0){
                    foreach ($v as $kk => $vv) {
                        $ary = array();
                        //print_r($vv);
                        if(!isset($vv['ApiMethod'])){
                            continue;
                        }
                        $ary['title'] = isset($vv['ApiTitle'])?$vv['ApiTitle'][0]:'';
                        $ary['method'] = isset($vv['ApiMethod'])?$vv['ApiMethod'][0]:'';
                        $ary['url'] = isset($vv['ApiRoute'])?$vv['ApiRoute'][0]:'';
                        if($ary['title']!=''){
                            $parentRuleList[$k][$kk] = $ary;
                        }
                        if($ary['url']!=''){
                            if($ary['url']==$this->request->get('url')){
                                //print_r($vv);exit;
                                $vv['url'] = str_replace('app/','',$vv['ApiRoute'][0]);
                                $this->view->assign("info", $vv);
                            }
                        }else{
                            $this->view->assign("info",$info);
                        }
                    } 
                }
            }
        }
        $this->view->assign("list", $parentRuleList);
        $routeurl = array();
        $routeurl['app\api\controller\Demo'] = '示例';
        $routeurl['app\api\controller\Ems'] = '邮件验证';
        $routeurl['app\api\controller\Index'] = '主页';
        $routeurl['app\api\controller\Sms'] = '短信验证';
        $routeurl['app\api\controller\Token'] = 'Token';
        $routeurl['app\api\controller\User'] = '会员管理';
        $routeurl['app\api\controller\Validate'] = '数据验证';
        $this->view->assign("routeurl",$routeurl);
        //return $this->view->fetch('index');
        return $this->view->fetch();
    }


}
?>